package com.formation.tocea.library.client.listener;

import java.util.Iterator;

import com.formation.tocea.library.client.ui.form.UpdateAccountForm;
import com.formation.tocea.library.client.ui.main.MainPanel;
import com.formation.tocea.library.client.ui.main.Menu;
import com.formation.tocea.library.client.ui.manager.BookManagerPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class UpdateMainPanelContentListener implements ClickHandler {
	public void onClick(ClickEvent event) {
		Button clickedButton = (Button) event.getSource();
		MainPanel mainPanel = (MainPanel) RootPanel.get("mainPanel").getWidget(0);
		Menu menu = (Menu) RootPanel.get("menu").getWidget(0);
		Widget newWidget = null;
		
		if (clickedButton.getText().equals("Modifier vos données")) {
			newWidget = new UpdateAccountForm();
		}
		else if (clickedButton.getText().equals("Gestion des livres")) {
			newWidget = new BookManagerPanel();
		}
		else if (clickedButton.getText().equals("Gestion des utilisateurs")) {
			
		}
		else if (clickedButton.getText().equals("Gestion des locations")) {
			
		}
		
		if (mainPanel.getActiveWidget() != null) {
			if (!mainPanel.getActiveWidget().equals(newWidget)) {
				mainPanel.remove(mainPanel.getActiveWidget());
				mainPanel.setActiveWidget(newWidget);
			}
		}
		else {
			mainPanel.setActiveWidget(newWidget);
		}

		//S'il existe des formulaires dans le menu, on les supprime
		Iterator<Widget> iterator = menu.iterator(); 
		while (iterator.hasNext()) {
			Widget widget = iterator.next();
			if (widget instanceof Grid) {
				menu.remove(widget);
			}
		}
	}
}
